package com.hc.appInfoActivity;

import java.util.List;

import com.hc.R;
import com.hc.util.fillData.AppInfoBean;
import com.hc.util.fillData.AppUserBean;



import android.content.Context;
import android.graphics.Color;
import android.provider.Settings.System;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class AppAdapter extends BaseExpandableListAdapter {

	private Context mContext;
	private List<AppInfoBean> data;
	 private LayoutInflater mInflater = null;
	 private String checkStr=null;
	public AppAdapter() {
	}
	public AppAdapter(Context context,List<AppInfoBean> data) {
		this.mContext = context;
		this.data = data;
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	

	public void setData(List<AppInfoBean> data) {
		this.data = data;
	}
	
	
	public List<AppInfoBean> getData() {
		return data;
	}
	
	public String getCheckStr() {
		return checkStr;
	}
	public void setCheckStr(String checkStr) {
		this.checkStr = checkStr;
	}
	@Override
	public Object getChild(int arg0, int arg1) {


		return null;
	}

	@Override
	public long getChildId(int arg0, int arg1) {
		return arg1;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		
		ChildViewHolder childViewHolder = null;

		if(convertView==null){
			
			 convertView = mInflater.inflate(R.layout.app_user_info_item, null);
			 childViewHolder = new ChildViewHolder();
			 childViewHolder.userNumber  = (TextView) convertView.findViewById(R.id.userNumber);
			 childViewHolder.userMac  = (TextView) convertView.findViewById(R.id.userMac);
			 childViewHolder.qiangdu  = (TextView) convertView.findViewById(R.id.userQiangdu);
			 childViewHolder.upIo  = (TextView) convertView.findViewById(R.id.userUpIo);
			 childViewHolder.downIo  = (TextView) convertView.findViewById(R.id.userDownIo);
			 convertView.setTag(childViewHolder);
		}else{
			childViewHolder = (ChildViewHolder) convertView.getTag();
		}
		AppUserBean bean = data.get(groupPosition).getUserBeans().get(childPosition);
		childViewHolder.userNumber.setText(""+bean.getId());
		childViewHolder.userMac.setText(bean.getUserMac());
		childViewHolder.qiangdu.setText(bean.getUserIntensity()+" dBm");
		childViewHolder.upIo.setText("上行流量："+(int)(Integer.parseInt(bean.getUpIo())/1024)+" kb/s");
		childViewHolder.downIo.setText("下行流量："+(int)(Integer.parseInt(bean.getDownIo())/1024)+" kb/s");
		
		
		return convertView;
		
		
		
	}

	@Override
	public int getChildrenCount(int arg0) {
		
		return data.get(arg0).getUserBeans().size();
	}

	@Override
	public Object getGroup(int arg0) {
		return data.get(arg0);
	}

	@Override
	public int getGroupCount() {
		return data.size();
	}

	@Override
	public long getGroupId(int arg0) {
		return arg0;
	}


	
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
		GroupViewHolder groupViewHolder = null;
	    if (convertView == null) {
            convertView = mInflater.inflate(R.layout.app_info_listview_content, null);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.app_channels= (TextView)convertView.findViewById(R.id.app_channels);
            groupViewHolder.app_name = (TextView)convertView.findViewById(R.id.app_name);
            groupViewHolder.app_pinglv =    (TextView)convertView.findViewById(R.id.app_rate);
            groupViewHolder.qiangdu2 = (ProgressBar)convertView.findViewById(R.id.app_bar);
            groupViewHolder.app_qiangdu = (TextView)convertView.findViewById(R.id.bar_value);
            groupViewHolder.chongchuanzhen = (TextView)convertView.findViewById(R.id.chongchuanzhen);
            groupViewHolder.chongchuanguanlizhen= (TextView)convertView.findViewById(R.id.chongchuanguanlizhen);
            groupViewHolder.shujuzhencount= (TextView)convertView.findViewById(R.id.shujuzhencount);
            groupViewHolder.shujuzhenlength= (TextView)convertView.findViewById(R.id.shujuzhenlength);
            groupViewHolder.crcshujuzhen= (TextView)convertView.findViewById(R.id.crcshujuzhen);
            groupViewHolder.physhujuzhen= (TextView)convertView.findViewById(R.id.physhujuzhen);
            groupViewHolder.guanlizhencount= (TextView)convertView.findViewById(R.id.guanlizhencount);
            groupViewHolder.guanlizhenlength= (TextView)convertView.findViewById(R.id.guanlizhenlength);
            groupViewHolder.crcguanlizhen= (TextView)convertView.findViewById(R.id.crcguanlizhen);
            groupViewHolder.phyguanlizhen= (TextView)convertView.findViewById(R.id.phyguanlizhen);
            groupViewHolder.beacon= (TextView)convertView.findViewById(R.id.beacon);
            groupViewHolder.upIo= (TextView)convertView.findViewById(R.id.upIo);
            groupViewHolder.downIo= (TextView)convertView.findViewById(R.id.downIo);
            groupViewHolder.userCount= (TextView)convertView.findViewById(R.id.userCount);
            convertView.setTag(groupViewHolder);
          
        }else{
        	groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
		
	    AppInfoBean dome = data.get(groupPosition);
	    groupViewHolder.app_channels.setText(" "+dome.getChain());
	    groupViewHolder.app_name.setText(dome.getWifiName()+"  ["+dome.getWifiMac()+"]");
	    groupViewHolder.app_pinglv.setText(dome.getFrequency()+"MHz");
		groupViewHolder.qiangdu2.setProgress(dome.getIntensity());
		groupViewHolder.app_qiangdu.setText(""+dome.getIntensity()+"dBm");
		groupViewHolder.chongchuanzhen.setText("重传帧："+dome.getChongchuanzhen());
        groupViewHolder.chongchuanguanlizhen.setText("重传管理帧："+dome.getChongchuanguanlizhen());
      
        groupViewHolder.shujuzhencount.setText("数据帧总数："+dome.getShujuzhenCount());
        groupViewHolder.shujuzhenlength.setText("数据帧长度："+dome.getShujuzhenLiengths());
        groupViewHolder.crcshujuzhen.setText("CRC数据帧："+dome.getCrcshujuzhen());
        groupViewHolder.physhujuzhen.setText("PHY数据帧："+dome.getPhyshujuzhen());
        groupViewHolder.guanlizhencount.setText("管理帧总数 ："+dome.getGuanlizhenCount());
        groupViewHolder.guanlizhenlength.setText("管理帧总长 ："+dome.getGuanlizhenLengths());
        groupViewHolder.crcguanlizhen.setText("CRC管理帧："+dome.getCrcguanlizhen());
        groupViewHolder.phyguanlizhen.setText("PHY管理帧："+dome.getPhyguanlizhen());
        groupViewHolder.beacon.setText("beacon帧间隔："+dome.getBeacon());
        int upIoCount = 0;
        int downIoCount =0; 
        for(AppUserBean userBean:dome.getUserBeans()){
        	upIoCount = upIoCount+Integer.parseInt(userBean.getUpIo());
        	downIoCount= downIoCount+Integer.parseInt(userBean.getDownIo());
        }
        groupViewHolder.upIo.setText("上行流量："+(int)(upIoCount/1024)+" kb/s");
        groupViewHolder.downIo.setText("下行流量："+(int)(downIoCount/1024)+" kb/s");
        groupViewHolder.userCount.setText("用户总数："+dome.getUserBeans().size());
        
        convertView.setBackgroundColor(Color.WHITE);
        groupViewHolder.app_name.setTextColor(Color.BLACK);
        if((java.lang.System.currentTimeMillis() - dome.getAddTime())>20000){
        	
        	 groupViewHolder.app_name.setTextColor(Color.RED);
        }
        
        if(checkStr!=null){
        	
        	
        	if((dome.getWifiMac()+dome.getChain()).equals(checkStr)){
        		
        		convertView.setBackgroundColor(Color.GRAY);
        	}
   
        }
        
      
        
		return convertView;

	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}

}
